package com.mt.components.utils {
	
	import mx.controls.DateField;
	import mx.formatters.DateFormatter;
	
	/**      
	 * 文件名称：DateUtils<br>
	 * 文件描述：<br>
	 * 创建人：马腾<br>
	 * 创建时间：Nov 25, 2012 8:38:34 PM<br>
	 * 修改人：马腾<br>
	 * 修改时间：Nov 25, 2012 8:38:34 PM<br>
	 */
	public class DateUtils {
		
		public static const MILLISECOND:Number=1;  
		public static const SECOND:Number = MILLISECOND * 1000;  
		public static const MINUTE:Number = SECOND * 60;  
		public static const HOUR:Number = MINUTE * 60;  
		public static const DAY:Number = HOUR * 24;  
		public static const WEEK:Number = DAY * 7;  
		public static const CHINESE_MONTH_FORMAT:String = "YYYY-MM";  
		public static const CHINESE_DATE_FORMAT:String = "YYYY-MM-DD";  
		public static const CHINESE_TIME_FORMAT:String = "HH";  
		public static const CHINESE_ALL_DATE_FORMAT:String="YYYY-MM-DD JJ:NN:SS";    
		
		/** 
		 * 字符串转换成日期类型
		 * @param dateString 时间字符串
		 * @param formatString 格式
		 * @return  
		 */  
		public static function stringToDate(dateString:String, formatString:String):Date  
		{  
			return DateField.stringToDate(dateString,formatString);
		}  
		
		/** 
		 * 日期转换成字符串类型
		 * @param date 
		 * @param formatString 
		 * @return  
		 */  
		public static function dateToString(date:Date,formatString:String):String{  
			//需要as3corelib.swc
			if(date == null)return null;
			var dateFormater:DateFormatter=new DateFormatter();
			dateFormater.formatString=formatString;  
			return dateFormater.format(date);  
		}  
		
		/**
		 * 给指定时间增加月 
		 * @param date
		 * @param addMonth
		 * @return 
		 */
		public static function addMonthByDate(date:Date , addMonth:Number):Date{
			var returnDate:Date=new Date(date.getTime());  
			returnDate.setMonth(returnDate.getMonth() + addMonth, returnDate.getDate());  
			return returnDate;  
		}
		
		/** 
		 * 给指定时间添加天数或减少天数 
		 * @param date 
		 * @param addDay 
		 * @return 
		 */  
		public static function addDaysByDateTime(date:Date, addDay:Number):Date  
		{  
			return new Date(date.getTime() + addDay * DAY);  
		} 
		
		/**
		 * 给指定时间增加小时 
		 * @param date
		 * @param addHour
		 * @return 
		 */		
		public static function addHourByDateTime(date:Date , addHour:Number):Date{
			return new Date(date.getTime() + addHour * HOUR);
		}
		
		/**
		 *给指点的时间添加分钟数 
		 * @param date
		 * @param addMin
		 * @return 
		 */		
		public static function addMinByDateTime(date:Date , addMin:Number):Date{
			return new Date(date.getTime() + addMin * MINUTE);
		}
		
		/**
		 * 给指定时间增加秒
		 * @param date
		 * @param addSecond
		 * @return 
		 */		
		public static function addSecondByDateTime(date:Date , addSecond:Number):Date{
			return new Date(date.getTime() + addSecond * SECOND);
		}
		
		/** 
		 * 取下一天 
		 */  
		public static function  getNextDay(currentDate:Date):Date{  
			return addDaysByDateTime(currentDate,1);  
		}  
		
		/** 
		 * 取上一天 
		 */  
		public static function  getLastDay(currentDate:Date):Date{  
			return addDaysByDateTime(currentDate,-1);  
		}  
		
		/** 
		 * 取下一个月 
		 */  
		public static function getNextMonth(currentDate:Date):Date  
		{  
			var returnDate:Date=new Date(currentDate.getTime());  
			returnDate.setMonth(returnDate.getMonth() + 1, returnDate.getDate());  
			return returnDate;  
		}  
		
		/** 
		 * 取上一个月 
		 */  
		public static function getLastMonth(currentDate:Date):Date  
		{  
			var returnDate:Date=new Date(currentDate.getTime());  
			returnDate.setMonth(returnDate.getMonth() - 1, returnDate.getDate());  
			return returnDate;  
		}  
		
		/** 
		 * 取下一个年 
		 */  
		public static function getNextYear(currentDate:Date):Date  
		{  
			var returnDate:Date=new Date(currentDate.getTime());  
			returnDate.setFullYear(returnDate.getFullYear() + 1);  
			return returnDate;  
		}  
		
		/** 
		 * 取上一个年 
		 */  
		public static function getLastYear(currentDate:Date):Date  
		{  
			var returnDate:Date=new Date(currentDate.getTime());  
			returnDate.setFullYear(returnDate.getFullYear() - 1);  
			return returnDate;  
		}  
		
		
		/** 
		 * 取下个月1号 
		 */  
		public static function getFristDayOfMonth(currentDate:Date):Date  
		{  
			currentDate.setMonth(currentDate.getMonth(), 1); //下个月的第一天，也就是下个月1号  
			return currentDate;  
		}  
		
		/** 
		 * 取当月月底 
		 */  
		public static function getLastDayOfMonth(currentDate:Date):Date  
		{  
			currentDate.setMonth(currentDate.getMonth() + 1, 1); //下个月的第一天，也就是下个月1号  
			currentDate.setDate(currentDate.getDate() - 1); //下个月1号之前1天，也就是本月月底  
			return currentDate;  
		}  
		
		/**
		 * 取得某个时间所在月份的第一天
		 * @param current
		 * @return 
		 */		
		public static function getFistDayOfCurrentMonth(current:Date):Date{
			current.setMonth(current.getMonth(), 1); 
			return current;
		}
		
		
		/** 
		 * 获取日期的中文表示方式：例如星期一\星期二之类的。(注意0表示星期天) 
		 * @param currentDate 
		 * @return 
		 */  
		public static function getChineseDay(currentDate:Date):String  
		{  
			switch (currentDate.getDay())  
			{  
				case 0:  
					return "星期日";  
					
				case 1:  
					return "星期一";  
					
				case 2:  
					return "星期二";  
					
				case 3:  
					return "星期三";  
					
				case 4:  
					return "星期四";  
					
				case 5:  
					return "星期五";  
					
				case 6:  
					return "星期六";  
					
				default:  
					return "";  
			}  
		}  
		
		/** 
		 * 获取日期的中文表示方式：例如星期一\星期二之类的。(注意0表示星期天) 
		 * @param currentDate 
		 * @return 
		 */  
		public static function getEnglishDay(currentDate:Date):String  
		{  
			switch (currentDate.getDay())  
			{  
				case 0:  
					return "Sunday";  
					
				case 1:  
					return "Monday";  
					
				case 2:  
					return "Tuesday";  
					
				case 3:  
					return "Wednesday";  
					
				case 4:  
					return "Thursday";  
					
				case 5:  
					return "Friday";  
					
				case 6:  
					return "Saturday";  
					
				default:  
					return "";  
			}  
		}  
	}
}